package com.ruoyi.use.aspect;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.use.domain.ExaminationUse;
import com.ruoyi.use.service.IExaminationUseService;
import com.ruoyi.examination.service.IExaminationService;
import com.ruoyi.examination.vo.ExaminationVO;
import com.ruoyi.storeroomuse.service.IStoreroomUseService;
import com.ruoyi.storeroomuse.vo.StoreUseVO;
import com.ruoyi.system.domain.Storeroom;
import com.ruoyi.system.service.IStoreroomService;
import com.ruoyi.system.vo.StoreroomAndUserVO;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// 离场
@Aspect
@Component
public class ExamOutAspect {

    @Autowired
    IExaminationUseService examinationUseService;

    @Autowired
    IExaminationService examinationService;

    @Autowired
    IStoreroomUseService storeroomUseService;
    @Autowired
    IStoreroomService storeroomService;

    @Pointcut("execution(public * com.ruoyi.examination.service.IExaminationService.out(..))")
    public void myMethod(){
        //代理方法
    }

    @Before("myMethod()")
    public void beforeMethod(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        for (Object every : args) {
            if (every instanceof String){
                String eve=(String) every;
                String[] strings = Convert.toStrArray(eve);
                for (String str: strings) {
                    ExaminationVO examinationVO = examinationService.selectExaminationByExaminationId(Long.parseLong(str));
                    SysUser sysUser = ShiroUtils.getSysUser();
                    StoreUseVO storeUseVO = new StoreUseVO();
                    storeUseVO.setStoreroomId(examinationVO.getStoreroomId());
                    storeUseVO.setExaminationId(examinationVO.getExaminationId());
                    storeUseVO.setFounder(sysUser.getUserId());
                    storeUseVO.setOperationName(4L);
                    storeroomUseService.insertStoreroomUse(storeUseVO);

                    // 判断库房是否满了
                    Long num = examinationService.selectNumByStoreId(examinationVO.getStoreroomId());
                    StoreroomAndUserVO storeroomAndUserVO = storeroomService.selectStoreroomByStoreroomId(examinationVO.getStoreroomId());
                    Long specification = storeroomAndUserVO.getSpecification();
                    if ((num-1) < specification.intValue()) {
                        Storeroom storeroom = new Storeroom();
                        storeroom.setStoreroomId(storeroomAndUserVO.getStoreroomId());
                        storeroom.setUseStatus(0);
                        storeroomService.updateStoreroom(storeroom);
                    }
                }
            }
        }
    }

    @After("myMethod()")
    public void afterMethod(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        for (Object every : args) {
            if (every instanceof String){
                String eve=(String) every;
                String[] strings = Convert.toStrArray(eve);
                for (String str: strings) {
                    ExaminationUse examinationUse = new ExaminationUse();
                    ExaminationVO examinationVO = examinationService.selectExaminationByExaminationId(Long.parseLong(str));
                    examinationUse.setExaminationId(examinationVO.getExaminationId());
                    SysUser sysUser = ShiroUtils.getSysUser();
                    examinationUse.setFounder(sysUser.getUserId());
                    examinationUse.setOperationName(6L);
                    examinationUseService.insertExaminationUse(examinationUse);
                }
            }
        }
    }
}
